/*
剑指 Offer II 007. 数组中和为 0 的三个数
给定一个包含 n 个整数的数组 nums，判断 nums 中是否存在三个元素 a ，b ，c ，使得 a + b + c = 0 ？请找出所有和为 0 且 不重复 的三元组。

 

示例 1：

输入：nums = [-1,0,1,2,-1,-4]
输出：[[-1,-1,2],[-1,0,1]]
示例 2：

输入：nums = []
输出：[]
示例 3：

输入：nums = [0]
输出：[]
 

提示：

0 <= nums.length <= 3000
-105 <= nums[i] <= 105
 
注意：本题与主站 15 题相同：https://leetcode-cn.com/problems/3sum/
*/

#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums)
    {
        vector<vector<int>> retVec;
        int length = nums.size();
        if(nums.size() < 3)
        {
            return retVec;
        }
        
        std::sort(nums.begin(),nums.end());
        
        for(int i = 0;i < length-2;++i)
        {
            //remove forward duplicate num
            if(i > 0 && nums[i] == nums[i-1])
            {
                continue;
            }
            int target = -nums[i];
            int front = i + 1;
            int back = length - 1;
            while(front < back)
            {
                int sum = nums[front] + nums[back];
                if(sum < target)
                {
                    ++front;
                }
                else if(sum > target)
                {
                    --back;
                }
                else
                {
                    retVec.push_back({nums[i],nums[front],nums[back]});
                    int frontVal = nums[front];
                    int backVal = nums[back];
                    //remove duplicate num from front
                    while(front < back && nums[front + 1] == frontVal)
                    {
                        ++front;
                    }
                    
                    //remove duplicate num from back
                    while(front < back && nums[back-1] == backVal)
                    {
                        --back;
                    }
                    
                    ++front;
                }
            }
        }
    
        return retVec;
    }
    
    vector<vector<int>> threeSum2(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int n = nums.size();
        vector<vector<int>> result;
        for(int p1 = 0; p1 < n-2; ++p1)
        {
            //remove forward duplicate 
            if(p1 > 0 && nums[p1] == nums[p1 - 1])
            {
                continue;
            }
            int p2 = p1 + 1;
            int p3 = n - 1;
            while(p2 < p3)
            {
                int sum = nums[p1] + nums[p2] + nums[p3];
                if(sum == 0)
                {
                    result.push_back({nums[p1],nums[p2],nums[p3]});
                    --p3;

                    //remove backward duplicate
                    while(p2 < p3 && nums[p3] == nums[p3+1])
                    {
                        --p3;
                    }
                }
                else if(sum < 0)
                {
                    ++p2;
                }
                else
                {
                    --p3;
                }
            }
        }

        return result;
    }
};